package it.enea.xlab.firstcrud.controllers;


import java.io.File;
import java.io.Serializable;
import java.util.*;

import javax.faces.application.FacesMessage;
import javax.faces.component.UICommand;
import javax.faces.component.UIForm;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.naming.InitialContext;

import it.enea.xlab.common.jsf.components.ComboBox;
import it.enea.xlab.common.jsf.components.Item;


import it.enea.xlab.firstcrud.crudmanager.CRUDManagerRemote;
import it.enea.xlab.firstcrud.entity.Supplier;
import it.enea.xlab.firstcrud.entity.TranslationType;
import it.enea.xlab.firstcrud.entity.TransmissionType;
import it.enea.xlab.firstcrud.utils.ContextUtils;


public class RegisterController extends AbstractCrudController implements Serializable{

	private static final long serialVersionUID = 1L;
	
	/** The current contact that is being edited. */
	private Supplier supplier = new Supplier();
	private String tempTransmissionTypeDescription, tempTranslationTypeDescription;
	
	/** menulist */
	public List<SelectItem> transmissionMenuList, translationMenuList;

	
	/** Supplier to remove. */
	private Supplier selectedSupplier;
	
	private Long selectedSupplierId;
	
	protected Supplier[] supplierRegister;
 
    protected CRUDManagerRemote crudManager;
    
	/** Add new link. */
	private UICommand addNewCommand;    
    
	/** The current form. */
	private UIForm form; 
	
	/** Persist command. */
	private UICommand persistCommand;

	public RegisterController() {

		this.supplier = new Supplier();
    	this.crudManager = getCrudManager();
		this.supplierRegister = readSupplierRegisterJPA();
		
		this.transmissionMenuList = ComboBox.createTransmissionMenuList();
		this.translationMenuList = ComboBox.createTranslationMenuList();	
	}

	
	public static CRUDManagerRemote getCrudManager() {
		
		CRUDManagerRemote myBean = null;
		InitialContext ctx;
		try {
			ctx = ContextUtils.getInitialContext("localhost:1099");		
			myBean = (CRUDManagerRemote) ctx.lookup("first-crud-ear-1.0-SNAPSHOT/CRUDManagerImpl/remote");
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return myBean;
	}
	
	private Supplier[] readSupplierRegisterJPA(){
	
    	HashMap<Long, Supplier> register = crudManager.getRegister();
 
		ArrayList<Supplier> list=new ArrayList(register.values());
		//Iterator<Supplier> iterator=list.iterator();
		Supplier[] tempSupplierRegister = new Supplier[list.size()];
	
		int i=0;
		for (Supplier su: list) {
			tempSupplierRegister[i++] = su;
		}
    	
        return tempSupplierRegister;
    }  
	
	
	public String addNew() {
		
	    System.out.println("RegisterController.addNew()");
		form.setRendered(true);
		addNewCommand.setRendered(false);
		persistCommand.setValue("Add");
		
		// integrazione Cristiano Novelli
		this.supplier = new Supplier(); 
		this.tempTranslationTypeDescription = null;
		this.tempTransmissionTypeDescription = null;
		
		super.setEdit(true);
		return null;
	}

	public String persist() {	    
		
		/* Turn form off, turn link on. */
		form.setRendered(false);
		addNewCommand.setRendered(true);
		
		supplier.setTranslationType(new TranslationType(this.tempTranslationTypeDescription));
		supplier.setTransmissionType(new TransmissionType(this.tempTransmissionTypeDescription));
		
		if (!crudManager.updateSupplier(supplier)) {
			addStatusMessage("ADDED Supplier with Id: " + supplier.getSupplierId() + "!");
		} else {
			addStatusMessage("UPDATED Supplier with Id: " + supplier.getSupplierId() + "!");
		}
		super.setEdit(false);
		
		// update supplier register reading through JPA
		updateSupplierRegister();
		
		return "contactPersisted";
	}

	
	public void remove() {

		if ( crudManager.deleteSupplier(this.selectedSupplierId) ) {
			
			/* adjust register */
			updateSupplierRegister();
			addStatusMessage("REMOVED Supplier with Id: " + this.selectedSupplierId + "!");
		}
		else
			addStatusMessage("Remove Action Error!");
		
		return;
	}
	
	
	public void read() {  
            
		/* Prepare selected supplier. */
		this.supplier = crudManager.readSupplier(this.selectedSupplierId);
		
		/* integrazione Cristiano Novelli */
		this.tempTranslationTypeDescription = this.supplier.getTranslationType().getTranslationTypeDescription();
		this.tempTransmissionTypeDescription = this.supplier.getTransmissionType().getTransmissionTypeDescription();
		
		/* Turn form on and the link off. */
		form.setRendered(true);
		addNewCommand.setRendered(false);
				
		addStatusMessage("Read " + supplier.getSupplierId());
		persistCommand.setValue("Update");
		super.setEdit(true);
	}
	
	private void addStatusMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_INFO, message, null));
	}

	public Supplier[] getSupplierRegister(){
    	
		if (this.supplierRegister.length == 0)
			addStatusMessage("No Suppliers in the Supplier Register!");
		
        return this.supplierRegister;
    }

	public void updateSupplierRegister() {
    	
    	
		this.supplierRegister = readSupplierRegisterJPA();		
        return;
    }
	
	
	public void cancel () {
        System.out.println("RegisterController.cancel()");	    
		form.setRendered(false);
		addNewCommand.setRendered(true);		
	}
	
	/* ------------------- Just properties. ------------------------- */
	
	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}
	
	public UIForm getForm() {
		return form;
	}

	public void setForm(UIForm form) {
		this.form = form;
	}
	
	public UICommand getAddNewCommand() {
		return addNewCommand;
	}
	
    public void setAddNewCommand(UICommand addNewCommand) {
		this.addNewCommand = addNewCommand;
	}
  
	public Supplier getSelectedSupplier() {
		return selectedSupplier;
	}

	public void setSelectedSupplier(Supplier selectedSupplier) {
		this.selectedSupplier = selectedSupplier;
	}

	public UICommand getPersistCommand() {
		return persistCommand;
	}

	public void setPersistCommand(UICommand persistCommand) {
		this.persistCommand = persistCommand;
	}

    @Override
    Object getFormObject() {
        if (super.isEdit()) {
            return this.supplier;
        } else {
            return null;
        }
    }

	public Long getSelectedSupplierId() {
		return selectedSupplierId;
	}

	public void setSelectedSupplierId(Long selectedSupplierId) {
		this.selectedSupplierId = selectedSupplierId;
	}

	public String getTempTransmissionTypeDescription() {
		return tempTransmissionTypeDescription;
	}

	public void setTempTransmissionTypeDescription(
			String tempTransmissionTypeDescription) {
		this.tempTransmissionTypeDescription = tempTransmissionTypeDescription;
	}

	public String getTempTranslationTypeDescription() {
		return tempTranslationTypeDescription;
	}

	public void setTempTranslationTypeDescription(
			String tempTranslationTypeDescription) {
		this.tempTranslationTypeDescription = tempTranslationTypeDescription;
	}

	public List<SelectItem> getTransmissionMenuList() {
		return transmissionMenuList;
	}

	public void setTransmissionMenuList(List<SelectItem> transmissionMenuList) {
		this.transmissionMenuList = transmissionMenuList;
	}


	public List<SelectItem> getTranslationMenuList() {
		return translationMenuList;
	}


	public void setTranslationMenuList(List<SelectItem> translationMenuList) {
		this.translationMenuList = translationMenuList;
	}
    
	
}


